#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <pthread.h>
#include <semaphore.h>


sem_t semr,semw;

int data = 0;

void *fun1(void *arg)
{

	while(1)
	{
		sem_wait(&semw);
		data = data + 1;
		sem_post(&semr);
	}
}

void *fun2(void *arg)
{
	while(1)
	{
		sem_wait(&semr);
		printf("data = %d\n",data);
		sem_post(&semw);
		
	}


}

int main()
{

	pthread_t tid1 = 0,tid2 = 0;
	int ret = 0;

	sem_init(&semw,0,1);
	sem_init(&semr,0,0);

	ret = pthread_create(&tid1,NULL,fun1,NULL);
	if(ret != 0)
	{
		perror("pthread_create");
		exit(-1);
	}

	ret = pthread_create(&tid2,NULL,fun2,NULL);
	if(ret != 0)
	{
		perror("pthread_create");
		exit(-1);
	}
	
	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);

	sem_destroy(&semr);
	sem_destroy(&semw);


	return 0;

}





